package demo.java.util;

import org.junit.Test;

import java.util.Random;

public class RandomDemo {

    private static Random random = new Random();

    @Test
    public void test() {
        Random random = new Random();
        for (int i = 0; i < 1000; i++) {
            System.out.println(random.nextInt(5));
        }
    }

    public static boolean nextBoolean() {
        return random.nextBoolean();
    }

    /**
     * 设置概率生成TRUE
     *
     * @param probability 生成TRUE的概率，如果大于0，则每次都返回true;如果小于0，每次都返回false;
     * @return
     */
    public static boolean nextTrue(double probability) {
        if (probability > 1.0) {
            return true;
        }
        if (probability < 0.0) {
            return false;
        }
        return Math.random() < probability;
    }

    @Test
    public void testNextTrue() {
        int i = 0;
        while (i < 100_0000) {
            i++;
            if (nextTrue(0.00001)) {
                System.out.println(i);
            }
        }
    }


    @Test
    public void random() {
        System.out.println(System.currentTimeMillis() / 1000 / 60);
        System.out.println(System.currentTimeMillis() / (1000 * 60));
        Random random = new Random();
        int i = 1;
        while (i-- > 0) {
            System.out.println(random.nextInt(9999));
        }
    }

}
